package com.jzcn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzcn.common.base.BaseServiceImpl;
import com.jzcn.common.dto.system.tool.SearchToolDTO;
import com.jzcn.common.dto.system.tool.ToolListDTO;
import com.jzcn.common.dto.system.tool.ToolRecordDTO;
import com.jzcn.common.utils.BeanUtils;
import com.jzcn.common.utils.JzPage;
import com.jzcn.domain.AppTool;
import com.jzcn.mapper.AppToolMapper;
import com.jzcn.service.AppToolService;
import com.jzcn.service.SysFileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 梦里小白龙
 * @version 0.0.1
 * @date 2019/12/5 13:59
 */
@Service
public class AppToolServiceImpl extends BaseServiceImpl<AppToolMapper, AppTool> implements AppToolService {

    @Resource
    AppToolMapper appToolMapper;

    @Resource
    SysFileService sysFileService;

    @Override
    public JzPage<ToolListDTO> listPage(SearchToolDTO dto) {
        IPage<ToolListDTO> iPage = new Page<>(dto.getPageNum(), dto.getPageSize());
        List<ToolListDTO> list = appToolMapper.toolPage(iPage,dto);
        list.forEach(e->{
            String url = sysFileService.getUrl(e.getFileId().intValue());
            if(!StringUtils.isEmpty(url)){
                e.setFileUrl(url);
            }
        });
        iPage.setRecords(list);
        return new JzPage<>(iPage);
    }

    @Override
    public Boolean create(ToolRecordDTO dto) {
        AppTool appTool = new AppTool();
        BeanUtils.copyProperties(dto,appTool);
        return super.save(appTool);
    }

    @Override
    public Boolean update(ToolRecordDTO dto) {
        AppTool appTool = new AppTool();
        BeanUtils.copyProperties(dto,appTool);
        return super.update(appTool);
    }

    @Override
    public boolean delete(Integer id) {
        return super.remove(id);
    }

    @Override
    public int finByCategoryIdCount(Integer id) {
        LambdaQueryWrapper<AppTool> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppTool::getCategoryId, id);
        return super.count(wrapper);
    }
}
